import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/**
 * Display User account data in a table and allow user to change authorization and freeze status
 *
 */
public class PersonalDataMangementUserGui extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jlblTitle = null;
	private JLabel jlblUserName = null;
	public JTextField jtxtUserName = null;
	public JButton jbtnSearch = null;
	public JButton jbtnBack = null;
	private JScrollPane jScrollPane = null;
	public JTable jTable = null;
	public JComboBox authorozation = null;
	private JLabel aznLabel = null;
	private JLabel FrzLabel = null;
	public JCheckBox frz = null;
	/**
	 * Name to the columns in the table
	 */
	public String[] ColumnNames={"ID","Password","Username","Phone Number","Address","Age","Name","Authorozation","Frozen"};
	/**
	 * hold the data about select user
	 */
	public String[][] data={};
	public DefaultTableModel model;
	public JButton save = null;
	public Boolean isUserR;
	private JButton jbtnLogout = null;
	
	/**
	 * This is the default constructor
	 */
	public PersonalDataMangementUserGui(String[][] d,Boolean a) {
		super();
		data=d;
		isUserR=a;
		initialize();
	}

	/**
	 *nitializes this
	 * 
	 * 
	 */
	private void initialize() {
		if(isUserR)
		{
			FrzLabel = new JLabel();
			FrzLabel.setBounds(new Rectangle(29, 462, 132, 27));
			FrzLabel.setText("Freeze/Unfreeze:");
			aznLabel = new JLabel();
			aznLabel.setBounds(new Rectangle(28, 418, 136, 27));
			aznLabel.setText("Authorozations:");
		}
		jlblUserName = new JLabel();
		jlblUserName.setBounds(new Rectangle(30, 359, 73, 34));
		jlblUserName.setText("UserName:");
		jlblTitle = new JLabel();
		jlblTitle.setBounds(new Rectangle(230, 15, 122, 32));
		jlblTitle.setFont(new Font("Dialog", Font.BOLD, 18));
		jlblTitle.setText("User Search");
		this.setSize(681, 555);
		this.setLayout(null);
		this.add(jlblTitle, null);
		this.add(jlblUserName, null);
		this.add(getJtxtUserName(), null);
		this.add(getJbtnSearch(), null);
		this.add(getJbtnBack(), null);
		this.add(getJScrollPane(), null);
		if(isUserR)
		{
			this.add(getAuthorozation(), null);
			this.add(aznLabel, null);
			this.add(FrzLabel, null);
			this.add(getFrz(), null);
		}
		this.setVisible(true);
		this.add(getSave(), null);
		this.add(getJbtnLogout(), null);
	}

	/**
	 * initializes jtxtUserName	
	 * 	
	 * @return JTextField	
	 */
	private JTextField getJtxtUserName() {
		if (jtxtUserName == null) {
			jtxtUserName = new JTextField();
			jtxtUserName.setBounds(new Rectangle(181, 360, 142, 33));
		}
		return jtxtUserName;
	}

	/**
	 *initializes jbtnSearch and set it to perform a search
	 * 	
	 * @return JButton
	 */
	private JButton getJbtnSearch() {
		if (jbtnSearch == null) {
			jbtnSearch = new JButton();
			jbtnSearch.setBounds(new Rectangle(384, 360, 166, 33));
			jbtnSearch.setText("Search");
			jbtnSearch.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(jtxtUserName.getText().length()>0)	
						Main.pdmc.lsreachUseru(jtxtUserName.getText());
					else
						JOptionPane.showMessageDialog(null,"Write Username first","ERROR",JOptionPane.ERROR_MESSAGE);
			
				}
			});
			
		}
		return jbtnSearch;
	}

	/**
	 * initializes jbtnBack	and set it to go back
	 * 	
	 * @return JButton
	 */
	private JButton getJbtnBack() {
		if (jbtnBack == null) {
			jbtnBack = new JButton();
			if(isUserR)
				jbtnBack.setBounds(new Rectangle(367, 505, 92, 34));
			else
				jbtnBack.setBounds(new Rectangle(680, 120, 182, 32));
			jbtnBack.setText("Back");
			jbtnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.pdmc.exit();
				}
			});
		}
		return jbtnBack;
	}
	/**
	 * Clear jtxtUserName
	 */
	public void lClear()
	{
		jtxtUserName.setText("");
	}

	/**
	 *initializes jScrollPane	
	 * 	
	 * @return JScrollPane
	 */
	public JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(20, 68, 632, 278));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 *initializes jTable	
	 * 	
	 * @return JTable	
	 */
	public JTable getJTable() {
		if (jTable == null) {
			if(data!=null)
			{
				model=new DefaultTableModel(data,ColumnNames);
				jTable = new JTable(model);
				jTable.addMouseListener(new java.awt.event.MouseListener() {
					public void mousePressed(java.awt.event.MouseEvent e) 
					{
						if(isUserR) Main.pdmc.lUpdateu();
						else save.setEnabled(true);
					}
					public void mouseClicked(java.awt.event.MouseEvent e) {
					}
					public void mouseReleased(java.awt.event.MouseEvent e) {
					}
					public void mouseEntered(java.awt.event.MouseEvent e) {
					}
					public void mouseExited(java.awt.event.MouseEvent e) {
					}
				});
				TableColumn column=jTable.getColumnModel().getColumn(0);
				column.setPreferredWidth(50);
				column=jTable.getColumnModel().getColumn(1);
				column.setPreferredWidth(100);
				column=jTable.getColumnModel().getColumn(2);
				column.setPreferredWidth(100);
				column=jTable.getColumnModel().getColumn(3);
				column.setPreferredWidth(200);
				column=jTable.getColumnModel().getColumn(4);
				column.setPreferredWidth(100);
				column=jTable.getColumnModel().getColumn(5);
				column.setPreferredWidth(50);
				column=jTable.getColumnModel().getColumn(6);
				column.setPreferredWidth(150);
				column=jTable.getColumnModel().getColumn(7);
				column.setPreferredWidth(150);
				column=jTable.getColumnModel().getColumn(8);
				column.setPreferredWidth(100);
				
			}
			else
			{
				model=new DefaultTableModel(data,ColumnNames);
				jTable = new JTable(model);
			}	
		}
		return jTable;
	}

	/**
	 *initializes getAuthorozation	
	 * 	
	 * @return JComboBox	
	 */
	private JComboBox getAuthorozation() {
		if (authorozation == null) {
			String[] num={"1","2","3"};
			authorozation = new JComboBox(num);
			authorozation.setBounds(new Rectangle(179, 418, 141, 27));
			authorozation.setEnabled(false);
		}
		return authorozation;
	}

	/**
	 * initializes getFrz	
	 * 	
	 * @return JCheckBox
	 */
	private JCheckBox getFrz() {
		if (frz == null) {
			frz = new JCheckBox();
			frz.setBounds(new Rectangle(179, 460, 41, 25));
			frz.setEnabled(false);
		}
		return frz;
	}

	/**
	 * initializes getSave	and set it to save the data from the table in the database
	 * 	
	 * @return JButton	
	 */
	private JButton getSave() {
		if (save == null) {
			if(isUserR)
			{
				save = new JButton("Save Details");
				save.setBounds(new Rectangle(475, 506, 182, 32));
			}
			else
			{
				save = new JButton("Get Report");
				save.setBounds(new Rectangle(680, 170, 182, 32));
			}
			
			save.setEnabled(false);
			save.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					int row=jTable.getSelectedRow();
					if(isUserR)
					{
						Integer aut=new Integer(authorozation.getSelectedIndex()+1);
						Main.pdmc.lUpdatef((String)jTable.getValueAt(row, 2),aut.toString(), frz.isSelected(), row,(String)jTable.getValueAt(row, 0));
					}
					else
					{
						Main.rrc=new ReaderReportCONTROL((String)jTable.getValueAt(row, 2));
						Main.rrc.lsreachUseru();
						
					}
				}
			});
		}
		return save;
	}

	/**
	 *initializes jbtnLogout and set it to logout from the system;	
	 * 	
	 * @return JButton
	 */
	private JButton getJbtnLogout() {
		if (jbtnLogout == null) {
			jbtnLogout = new JButton();
			jbtnLogout.setBounds(new Rectangle(564, 360, 96, 33));
			jbtnLogout.setText("Logout");
			jbtnLogout.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout ?","Confirmation", 0) == 0)
					{
						Main.pdmc.exit();
						Main.welcomeScreen.panel.setVisible(false);
						Main.welcomeScreen.panel.setEnabled(false);
						Main.log.loginData=new String[3]; //GIL
						Main.log.loginData[0]=Main.gusers.id;
						Main.log.loginData[1]="logout";
						Main.log.loginData[2]=Main.gusers.id;
						Main.log.connection.accept("logout"); //GIL
						Main.log.flag=false;//GIL
						while(Main.log.flag==false);//GIL
						Main.log.flag=false;//GIL
						Main.log.user.setText("");
						Main.log.pass.setText("");
						Main.log.frame.setSize(650,550);
						Main.log.panel.setEnabled(true);
						Main.log.panel.setVisible(true);
					}
				}
			});
		}
		return jbtnLogout;
	}

} 
